package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class MessageSpotPeer extends BasePeer{
	public static final String tableName = "MessageSpot";
	public static final String USERID = "userId";
	public static final String RECEIVED = "received";
	public static final String UNREAD = "unread";
	public static final String SENT = "sent";
	public static final String DRAFT = "draft";
	public static final String NOFITY = "nofity";
	public static final String[] columnNames=new String[]{"userId","received","unread","sent","draft","nofity"};
	public static MessageSpot getMessageSpotByPKs(Object userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(MessageSpotPeer.tableName,MessageSpotPeer.columnNames," TRUE  AND `userId`=? ",new Object[]{userId,},0, 1,conn);
			MessageSpot messageSpot = new MessageSpot();
		fill(rs,messageSpot);
		return messageSpot;
	}
	public static MessageSpot getMessageSpotReferenceColumnsOnlyByPKs(Object userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(MessageSpotPeer.tableName,new String[]{"userId",}," TRUE AND `userId`=?",new Object[]{userId,}, 0,  1,conn);
			MessageSpot messageSpot = new MessageSpot();
		fill(rs,messageSpot);
		return messageSpot;
	}
	public static MessageSpot getMessageSpotByUserId(long userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(MessageSpotPeer.tableName,MessageSpotPeer.columnNames,"`userId`=?",new Object[]{userId},0, 1,conn);
			MessageSpot messageSpot = new MessageSpot();
		fill(rs,messageSpot);
		return messageSpot;
	}
	public static void fill(ResultSet rs, MessageSpot messageSpot) throws SQLException {
		if(rs.next()){
			try{messageSpot.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{messageSpot.setReceived(rs.getLong("received"));}catch(Exception ex){}
			try{messageSpot.setUnread(rs.getLong("unread"));}catch(Exception ex){}
			try{messageSpot.setSent(rs.getLong("sent"));}catch(Exception ex){}
			try{messageSpot.setDraft(rs.getLong("draft"));}catch(Exception ex){}
			try{messageSpot.setNofity(rs.getString("nofity"));}catch(Exception ex){}
			messageSpot.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<MessageSpot> messageSpotCollection) throws SQLException {
		while(rs.next()){
			MessageSpot messageSpot=new MessageSpot();
			try{messageSpot.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{messageSpot.setReceived(rs.getLong("received"));}catch(Exception ex){}
			try{messageSpot.setUnread(rs.getLong("unread"));}catch(Exception ex){}
			try{messageSpot.setSent(rs.getLong("sent"));}catch(Exception ex){}
			try{messageSpot.setDraft(rs.getLong("draft"));}catch(Exception ex){}
			try{messageSpot.setNofity(rs.getString("nofity"));}catch(Exception ex){}
			messageSpot.resetModified();
			messageSpotCollection.add(messageSpot);
		}
	}
	public static boolean update(MessageSpot messageSpot, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < MessageSpotPeer.columnNames.length; i++) {
			if (((MessageSpotBase)messageSpot).modified[i]) {
				columns.add(MessageSpotPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `MessageSpot` SET ");
		sql.append("WHERE TRUE AND `userId` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,messageSpot.get(column));
		}
		pstmt.setObject(columns.size()+1+0,messageSpot.get("userId"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(MessageSpot messageSpot, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (messageSpot.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `MessageSpot` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (messageSpot.get(column) != null) {
				pstmt.setObject(i + 1 + j, messageSpot.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<MessageSpot> getMessageSpotCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<MessageSpot> collection=new Vector<MessageSpot>();
		fill(rs,collection);
		return collection;
	}
}
